using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Framework.Library;
using Framework.Library.Components.Portal;
using Framework.Library.Components.Roles;
using Framework.Library.Components.Security;
using Framework.Library.Components.Users;
using Framework.Library.Utilities;
using Framework.Library.Components.Commands;

namespace Portal.Administrator.Roles
{
	public partial class UserInRole : AuthenticatedControl
	{
		protected void Page_Load(object sender, EventArgs e)
		{
            if ((!CurrentUser.IsSuperAdmin && CommandController.IsSystemModule(AppEnv.CommandID())) || !CommandController.IsAvailableForUser(AppEnv.CommandID(), CurrentUser.UserID, Convert.ToInt32(AppEnv.PortalId())))
                Response.Redirect(AppEnv.ADMIN_CMD, true);			
			if (!Page.IsPostBack)
			{
                lblCommandName.Text = AppEnv.CommandName();

                dropRole.Items.Clear();
                RoleController.LoadRoles(dropRole.Items);

                dropPortal.Items.Clear();                
                PortalController.LoadPortals(dropPortal.Items);
                LoadData();
			}

		}
        private void LoadData()
        {
            lstUsers.DataSource = UserController.GetUsersNotInRole(ConvertUtility.ToInt32(dropRole.SelectedValue), ConvertUtility.ToInt32(dropPortal.SelectedValue));
            lstUsers.DataBind();

            lstUserInRole.DataSource = UserController.GetUsers(ConvertUtility.ToInt32(dropRole.SelectedValue), ConvertUtility.ToInt32(dropPortal.SelectedValue));
            lstUserInRole.DataBind();
        }
		
		protected void btnAdd_Click(object sender, EventArgs e)
		{
            int roleId = Convert.ToInt32(dropRole.SelectedValue);
            try
            {
                foreach (ListItem item in lstUsers.Items)
                {
                    if (item.Selected)
                    {
                        RoleController.UpdateUserToRole(Convert.ToInt32(item.Value), roleId, ConvertUtility.ToInt32(dropPortal.SelectedValue));
                    }
                }
                LoadData();
                lblUpdateStatus.Text = MiscUtility.MSG_UPDATE_SUCCESS;
            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = ex.Message;
            }
		}
        protected void btnRemove_Click(object sender, EventArgs e)
        {
            int roleId = Convert.ToInt32(dropRole.SelectedValue);
            try
            {
                foreach (ListItem item in lstUserInRole.Items)
                {
                    if (item.Selected)
                    {
                        RoleController.RemoveUserFromRole(Convert.ToInt32(item.Value), roleId, ConvertUtility.ToInt32(dropPortal.SelectedValue));
                    }
                }
                LoadData();
                lblUpdateStatus.Text = MiscUtility.MSG_UPDATE_SUCCESS;
            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = ex.Message;
            }
        }

        protected void dropPortal_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadData();
        }

        protected void dropRole_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadData();
        }
	}
}